package com.example.a1.fannong.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.a1.fannong.R;

import cn.personal.library.viewinject.FindViewById;
import cn.personal.library.viewinject.InjectOnClickListener;
import cn.personal.library.viewinject.ViewInjecter;

/**
 * 程序actionbar
 */
public class MyActionBar extends RelativeLayout {
    public static final int FUNCTION_LEFT_BTN = 1;
    public static final int FUNCTION_TITLE = 2;
    public static final int FUNCTION_RIGHT_TEXT = 4;
    public static final int FUNCTION_RIGHT_BTN = 8;

    private int function;

    private View rootView;

    @FindViewById(R.id.action_bar_left_btn)
    private ImageView leftBtn;

    @FindViewById(R.id.action_bar_right_btn)
    private ImageView rightBtn;

    @FindViewById(R.id.action_bar_title)
    private TextView titleView;

    @FindViewById(R.id.action_bar_right_text)
    private TextView rightText;

    public MyActionBar(Context context) {
        this(context, null, 0);
    }

    public MyActionBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.view_action_bar, this, false);
        rootView = view;
        addView(view);
        ViewInjecter.inject(this, view);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyActionBar);
        int defColor = getResources().getColor(R.color.color_white);
        setActionBarBackground(typedArray.getColor(R.styleable.MyActionBar_barBackground, defColor));
        setTitle(typedArray.getString(R.styleable.MyActionBar_titleText));
        int defTitleColor = getResources().getColor(R.color.font_level_2_color);
        setTitleColor(typedArray.getColor(R.styleable.MyActionBar_titleColor, defTitleColor));

        setRightText(typedArray.getString(R.styleable.MyActionBar_rightText));
        setRightTextColor(typedArray.getColor(R.styleable.MyActionBar_rightTextColor, defTitleColor));
        setRightBtnImageDrawable(typedArray.getDrawable(R.styleable.MyActionBar_rightBtnImage));

        Drawable leftBtnImageDrawable = typedArray.getDrawable(R.styleable.MyActionBar_leftBtnImage);
        if (leftBtnImageDrawable == null) {
            leftBtnImageDrawable = getResources().getDrawable(R.drawable.ic_white_back);
        }
        setLeftBtnImageDrawable(leftBtnImageDrawable);

        int function = typedArray.getInt(R.styleable.MyActionBar_function, FUNCTION_LEFT_BTN | FUNCTION_TITLE);
        setFunction(function);

        String backClick = typedArray.getString(R.styleable.MyActionBar_leftClick);
        if (TextUtils.isEmpty(backClick)) {
            backClick = "onBackClick";
        }
        setLeftOnClickListener(new InjectOnClickListener(backClick, context));

        String titleClick = typedArray.getString(R.styleable.MyActionBar_titleClick);
        if (!TextUtils.isEmpty(titleClick)) {
            setTitleOnClickListener(new InjectOnClickListener(titleClick, context));
        }

        String rightClick = typedArray.getString(R.styleable.MyActionBar_rightTextClick);
        if (!TextUtils.isEmpty(rightClick)) {
            setRightTextOnClickListener(new InjectOnClickListener(rightClick, context));
        }

        String rightBtnClick = typedArray.getString(R.styleable.MyActionBar_rightBtnClick);
        if (!TextUtils.isEmpty(rightBtnClick)) {
            setRightBtnOnClickListener(new InjectOnClickListener(rightBtnClick, context));
        }

        typedArray.recycle();
    }

    public void setActionBarBackground(@ColorInt int color) {
        rootView.setBackgroundColor(color);
    }

    public void setTitle(String title) {
        titleView.setText(title);
    }

    public void setTitleColor(@ColorInt int color) {
        titleView.setTextColor(color);
    }

    public void setRightTextColor(@ColorInt int color) {
        rightText.setTextColor(color);
    }

    public void setFunction(int function) {
        this.function = function;

        leftBtn.setVisibility((function & FUNCTION_LEFT_BTN) == FUNCTION_LEFT_BTN ? VISIBLE : GONE);
        titleView.setVisibility((function & FUNCTION_TITLE) == FUNCTION_TITLE ? VISIBLE : GONE);
        rightText.setVisibility((function & FUNCTION_RIGHT_TEXT) == FUNCTION_RIGHT_TEXT ? VISIBLE : GONE);
        rightBtn.setVisibility((function & FUNCTION_RIGHT_BTN) == FUNCTION_RIGHT_BTN ? VISIBLE : GONE);
    }

    public void addFunction(int function) {
        setFunction(this.function | function);
    }

    public void removeFunction(int function) {
        setFunction(this.function & ~function);
    }

    public void setLeftOnClickListener(OnClickListener onClickListener) {
        leftBtn.setOnClickListener(onClickListener);
    }

    public void setRightBtnOnClickListener(OnClickListener onClickListener) {
        rightBtn.setOnClickListener(onClickListener);
    }

    public void setRightTextOnClickListener(OnClickListener onClickListener) {
        rightText.setOnClickListener(onClickListener);
    }

    public void setRightBtnImageDrawable(Drawable drawable) {
        rightBtn.setImageDrawable(drawable);
    }

    public void setLeftBtnImageDrawable(Drawable drawable) {
        leftBtn.setImageDrawable(drawable);
    }

    public void setRightText(String text) {
        rightText.setText(text);
    }

    public void setTitleOnClickListener(OnClickListener onClickListener) {
        titleView.setOnClickListener(onClickListener);
    }
}
